package research.ui.editors.tableeditors.strength;

import research.domain.StrengthValue;
import research.entity.Entity;
import research.ui.editors.tableeditors.EntityCellModifier;
import research.ui.editors.tableeditors.EntityLabelProvider;
import research.ui.editors.tableeditors.EntityTableEditor;

public class StrengthCellModifier extends EntityCellModifier {

	public StrengthCellModifier(EntityTableEditor viewer) {
		super(viewer);
	}
	
	public static double calcStrengthValue(StrengthValue par) {
		try {
			return par.getPower() * 100000 / par.getThickness();
		} catch (Exception e) {
			return Double.NaN;
		}
	}

	@Override
	protected Object getValue(Entity entity, int columnIndex) {
		StrengthValue par = (StrengthValue) entity;

		switch (columnIndex) {
		case 0:
			return EntityLabelProvider.render(par.getNumber());
		case 1:
			return EntityLabelProvider.render(par.getThickness());
		case 2:
			return EntityLabelProvider.render(par.getPower());
		case 3:
			return EntityLabelProvider.render(par.getElongation());
		case 4:
			double val = calcStrengthValue(par);
			return Double.isNaN(val) ? "" : EntityLabelProvider.render(val);
		default:
		}
		return null;
	}

	@Override
	protected void modify(Entity entity, int columnIndex, Object value) {
		StrengthValue par = (StrengthValue) entity;
		switch (columnIndex) {
		case 0:
			par.setNumber(parseInt(value));
			break;
		case 1:
			par.setThickness(parseDouble(value));
			break;
		case 2:
			par.setPower(parseDouble(value));
			break;
		case 3:
			par.setElongation(parseDouble(value));
			break;
		case 4:
			par.setStrengthValue(parseDouble(value));
			break;
		default:
		}
	}

	@Override
	public boolean canModify(Object element, String property) {
		return getColumnIndex(property) != 4;
	}
	
}
